package info.motteke.kurumi;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TextTest {

    private static final class TextLayoutListener implements Listener {
        private final Image[] images;
        private final TextLayout layout;
        private final int[] offsets;
        private final Composite comp;

        private TextLayoutListener(Composite comp, Image[] images, TextLayout layout, int[] offsets) {
            this.comp = comp;
            this.images = images;
            this.layout = layout;
            this.offsets = offsets;
        }

        @Override
        public void handleEvent(Event event) {
            switch (event.type) {
                case SWT.Paint :
                    paint(event);
                    break;

                case SWT.Resize :
                    resize(event);
                    break;
            }
        }

        private void paint(Event event) {
            GC gc = event.gc;

            layout.draw(gc, 0, 0);

            for(int i = 0; i < images.length; i++){
                int offset = offsets[i];
                Image image = images[i];

                int lineIndex = layout.getLineIndex(offset);
                FontMetrics font = layout.getLineMetrics(lineIndex);
                Point point = layout.getLocation(offset, false);
                GlyphMetrics glyph = layout.getStyle(offset).metrics;

                gc.drawImage(image, point.x, point.y + font.getAscent() - glyph.ascent);
            }
        }

        private void resize(Event event) {
            layout.setWidth(comp.getSize().x);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        shell.setText("テキストレイアウトのテスト");
        shell.setLayout(new FillLayout());
        ScrolledComposite comp = new ScrolledComposite(shell, SWT.V_SCROLL);
        comp.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        final TextLayout layout = new TextLayout(display);

        layout.setText("コメントのテストですよ。\uFFFC コメントのテストですよ。\uFFFC http://example.com");

        final int[] offsets = { 12, 26, };
        ClassLoader cl = TextTest.class.getClassLoader();

        final Image[] images = { new Image(display, cl.getResourceAsStream("246.gif")),
                                 new Image(display, cl.getResourceAsStream("57.gif")),
                               };

        for(int i = 0; i < images.length; i++) {
            Rectangle bounds = images[i].getBounds();
            TextStyle imageStyle = new TextStyle(null, null, null);
            imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
            layout.setStyle(imageStyle, offsets[i], offsets[i]);
        }

        Listener listener = new TextLayoutListener(comp, images, layout, offsets);
        comp.addListener(SWT.Paint, listener);
        comp.addListener(SWT.Resize, listener);

        shell.open();
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        layout.dispose();
        display.dispose();
    }

}
